/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/

#define OMEN_SOURCE 1

#include <OmenSphere.h>

using namespace Omen;

Sphere::Sphere() :
    m_position(0.0,0.0,0.0),
    m_radius(0.0)
{
}

Sphere::Sphere( const Omen::Vector3d& position, const Omen::Float radius ) :
    m_position(position),
    m_radius(radius)
{
}
    
Sphere::Sphere( const Sphere& sphere )
{
    m_radius = sphere.m_radius;
    m_position = sphere.m_position;
}


///
/// Returns a sphere defining a union of this and given other sphere
///
Sphere& Sphere::operator+(const Omen::Sphere& other)
{
    Omen::Float xmin, ymin, zmin;
    Omen::Float xmax, ymax, zmax;

    xmin = min(m_position.x-m_radius, other.m_position.x-other.m_radius);
    ymin = min(m_position.y-m_radius, other.m_position.y-other.m_radius);
    zmin = min(m_position.z-m_radius, other.m_position.z-other.m_radius);

    xmax = max(m_position.x+m_radius, other.m_position.x+other.m_radius);
    ymax = max(m_position.y+m_radius, other.m_position.y+other.m_radius);
    zmax = max(m_position.z+m_radius, other.m_position.z+other.m_radius);

    Omen::Vector3d vmin(xmin,ymin,zmin);
    Omen::Vector3d vmax(xmax,ymax,zmax);

    Omen::Vector3d vCenter = (vmin+vmax)/2.0f;
    Omen::Float radius = (vmax - vCenter).length();

    m_radius = radius;
    m_position = vCenter;

    return *this;
}

Sphere& Sphere::operator+=(const Omen::Sphere& other)
{
    *this = *this + other;
    return *this;
}

Sphere::~Sphere()
{
}

